package com.park.web.util.email;

import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.park.client.model.user.UserModel;
import com.park.common.constant.Constant;
import com.park.common.util.MD5;

public class EmailUtil {
	protected final Logger logger = LoggerFactory.getLogger(EmailUtil.class);
	private static final String host = "smtp.163.com";
	private static final String sender = "testcenter321@163.com";
	private static final String username = "testcenter321";
	private static final String password = "qazxsw";
	private static final String receiver = "1280453165@qq.com";
	private static final Properties pro = new Properties();
	static{
		pro.put("mail.smtp.host", host);
		pro.put("mail.smtp.auth", "true");
	}
	
	public static boolean sendEmail(String content){
		try {
				Session session=Session.getInstance(pro,new Authenticator() {
		
					@Override
					protected PasswordAuthentication getPasswordAuthentication() {
						return new PasswordAuthentication(username,password);
					}
					
				});
				 //封装Message对象
				session.setDebug(true);
		        Message message=new MimeMessage(session);
		        message.setFrom(new InternetAddress(sender)); //发件人
				message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
				message.setSubject("意见反馈");
				//设置内容(设置字符集处理乱码问题)
				message.setContent(content,"text/html;charset=utf-8");
		        message.setSentDate(new Date());
		      //创建Transport实例，发送邮件
		        Transport tran=session.getTransport("smtp");
		        tran.connect(host, username, password);
		        tran.sendMessage(message,message.getAllRecipients());
		        tran.close();
		        return true;
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	public static boolean sendEmail(String subiect,String receiver,String content){
		try {
			Session session=Session.getInstance(pro,new Authenticator() {
				@Override
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username,password);
				}
			});
			//封装Message对象
			session.setDebug(true);
			Message message=new MimeMessage(session);
			message.setFrom(new InternetAddress(sender)); //发件人
			message.setRecipient(Message.RecipientType.TO, new InternetAddress(receiver));
			message.setSubject(subiect);
			//设置内容(设置字符集处理乱码问题)
			message.setContent(content,"text/html;charset=utf-8");
			message.setSentDate(new Date());
			//创建Transport实例，发送邮件
			Transport tran=session.getTransport("smtp");
			tran.connect(host, username, password);
			tran.sendMessage(message,message.getAllRecipients());
			tran.close();
			return true;
		} catch (AddressException e) {
			e.printStackTrace();
			return false;
		} catch (MessagingException e) {
			e.printStackTrace();
			return false;
		}
	}
	
	/**
	 * 邮件内容
	 */
	public static String emailContent(UserModel u,String secretKey,HttpServletRequest request){
		
		StringBuffer content = new StringBuffer();
		//将用户名、过期时间、密钥生成链接密钥
		String key =u.getUsername() + "$" + u.getOutDate() + "$" + secretKey;
		
		String digitalSignature = MD5.md5(key);// 数字签名
		
//		String basePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
		
		String resetPassHref = Constant.BASEPATH + "/user/updatePassword?sid="+ digitalSignature +"&id="+u.getId();
		content.append("<div><div style='margin-left:4%;'>");  
		content.append("<p style='color:red;'>");  
        content.append(u.getChName()+"您好:</p>");  
        content.append("<p style='text-indent: 2em;'>您正在使用密码找回功能，请点击下面的链接完成密码找回。</p>");  
        content.append("<p style='text-indent: 2em;display: block;word-break: break-all;'>");  
        content.append("链接地址：<a style='text-decoration: none;' target='_black' href='"+resetPassHref+"'>"+resetPassHref+"</a></p>");  
        content.append("</div>");  
        content.append("<ul style='color: rgb(169, 169, 189);font-size: 18px;'>");  
        content.append("<li>为了保障您帐号的安全，该链接有效期为30分钟。</li>");  
        content.append("<li>该链接只能使用一次，请周知。</li>"); 
        content.append("<li>如果该链接无法点击，请直接复制以上网址到浏览器地址栏中访问。</li>");  
        content.append("<li>请您妥善保管，此邮件无需回复。</li>");  
        content.append("</ul>");  
        content.append("</div>");
		return content.toString();
}
	
	public static void main(String[] args) throws Exception {
		EmailUtil.sendEmail("哈哈");
}

}
